<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
class Role extends  Common{
    
    
    
    // 数据列表
    public function index() {
    	$list = D('admin_role')->select();
    	return view('role/index',['list'=>$list]);
    }
    
    
    
    
    
    // 添加数据
    public function add() {
    	if (request()->isPost()) {
    	    // 获取全部参数
    	    $postField = 'rolename,description,disabled';
			$data = request()->only(explode(',',$postField),'post',null);
			
    	    if(!$data['rolename']) Jump::error('角色名称不能为空!');
    	    // 插入数据
            $data['system'] = 1;
    		$r = D('admin_role')->insert($data);
           
            // 判断是否执行成功
            if($r){
    		   Jump::success("添加成功!");
    		}
    		else {
    		   Jump::error('添加错误!!');
    		}
    		
    	}
    	sleep(1);
    	return view('role/role_add');
    }
    
    
    
    
    
    //  编辑数据
    public function edit() {
    	$roleid = request()->param('roleid');
    	$find = D('admin_role')->where('roleid',$roleid)->find();
    	
    	if (request()->isPost()){
    	    // 获取全部参数
    	    $postField = 'roleid,rolename,description,disabled';
			$data = request()->only(explode(',',$postField),'post',null);
			
    	    $data['system'] = 1;
    		$r = D('admin_role')->where('roleid',$data['roleid'])->update($data);
    		// 判断是否执行成功
            if($r){
    		   Jump::success("更新成功!"); 
    		}
    		else {
    		    Jump::error('数据未改动!!'); 
    		}
    		
    	}
    	sleep(1);
    	return view('role/role_edit',['find'=>$find]);
    }
    
    
    
    // 权限管理
    public function role_priv() {
    	// 获取网页中提交的值
    	$roleid = request()->param('roleid');
    	// 	输出查询中的菜单名称
    	$list = D('app_menu')->order('sort dsc')->select();
    	// 	二次处理 无线分类
    	$lists = tree_menu($list);
    	// 	通过值 联合查询输出已经提交的值
    	$role_priv = D('admin_role_priv')->alias('a')->join('qh_app_menu w','a.typeid = w.id')->where(['roleid'=>$roleid])->select();
    	
    	if (request()->isPost()) {
    		// 获取全部参数
    	    $postField = 'fx,roleid';
			$k = request()->only(explode(',',$postField),'post',null);
			
			if(!$k['fx']) Jump::error('选项不能为空!');
			
    		if(count($k['fx']) > 0) {
    		    D('admin_role_priv')->where(['roleid'=>$k['roleid']])->delete();
    			// 循环处理通过post提交的值
    			foreach($k['fx'] as $val) {
    				//  查询菜单ID 并找到相关的值
    				$data = D('app_menu')->where('id',$val)->field('m,c,a,data')->find();
    				$r = D('admin_role_priv')->insert(['roleid'=>$k['roleid'],'typeid'=>$val,'m'=>$data['m'],'c'=>$data['c'],'a'=>$data['a'],'data'=>$data['data']]);
    			}
    		} else {
    			$r = D('admin_role_priv')->where(['roleid'=>$k['roleid']])->delete();
    		
    		}
    		Jump::success('提交成功!');
    	}
    	sleep(1);
    	return view('role/role_priv',['lists'=>$lists,'roleid'=>$roleid,'role_priv'=>$role_priv]);
    }
    
    
    
    
    
    
    // 删除数据
    public function del() {
    	if (request()->isPost()) {
    	   // 获取网页中提交的值
    	     $roleid = request()->param('roleid');
    	     if(empty($roleid))Jump::error('ID不存在!');
             $data = D('admin_role')->where('roleid',$roleid)->delete();
    	}
    	// 判断是否执行成功
    	if($data) {
    		Jump::success("删除成功!!");
    	} else {
    		Jump::error('删除失败!');
    	}
    }
    
    
    
    
    
}
